import Vue, { h } from "vue";
import VueRouter from "vue-router";
import Home from '../pages/Home/Home'
import Search from '../pages/Search/Search'
import Login from '../pages/Login/Login'
import Register from '../pages/Register/Register'
import Detail from '../pages/Detail/Detail'
import AddCartSuccess from  '@/pages/AddCartSuccess/AddCartSuccess'
import ShopCart from '@/pages/ShopCart/shopCart'
import store from '@/store'
import Trade from '@/pages/Trade'
import Pay from '@/pages/Pay'
import PaySuccess from '@/pages/PaySuccess'
import Center from '@/pages/Center'
import Myorder from '@/pages/Center/myOrder'
import Grouporder from '@/pages/Center/GroupOrder'
import shopcart from "@/store/shopcart/shopcart";

Vue.use(VueRouter);
const router = new VueRouter({
    routes: [
        {
            path: '/center',
            component: Center,
            meta: { show: true },
            children:[
                {
                    path:'myorder',
                    component:Myorder,
                    meta:{ show: true }
                },
                {
                    path:'grouporder',
                    component:Grouporder,
                    meta:{ show: true }
                },
                {
                    path:'/center',
                    //重定向
                    redirect:'/center/myorder'
                }
            ]
        },

        {
            path: '/paysuccess',
            component: PaySuccess,
            meta: { show: true },
            beforeEnter:(to,from,next)=>{
                if(from.path=='/pay'||to.path=='/pay'||to.path=='/paysuccess'){
                    next()
                }else{
                    next(false)
                }
            }
        },
        {
            path: '/pay',
            component: Pay,
            meta: { show: true },
            beforeEnter:(to,from,next)=>{
                if(from.path=='/trade'||from.path=='/paysuccess'||to.path=='/pay'){
                    next()
                }else{
                    next(false)
                }
            }
        },
        {
            path: '/trade',
            component: Trade,
            meta: { show: true },
            // beforeEnter:(to,from,next)=>{
            //     if(from.path=='/shopcart'){
            //         console.log(1111)
            //         next()
            //     }else{
            //         next(false)
            //     }
            // }
        },
        {
            path: '/detail/:skuid',
            component: Detail,
            meta: { show: true }
        },
        {
            path: '/home',
            component: Home,
            meta: { show: true }
        },
        {
            path: '/search/:keyword?',
            name: 'search',
            component: Search,
            meta: { show: true },
            
        },
        {
            path: '/login',
            component: Login,
            meta: { show: false }
        },
        {
            path: '/register',
            component: Register,
            meta: { show: false }
        },
        {   
            path:'/addcartsuccess/:skuNum?',
            name:'addcartsuccess',
            component:AddCartSuccess,
            meta:{show:true}

        },
        {
            
            path:'/shopcart/:skuNum?',
            name:'shopcart',
            component:ShopCart,
            meta:{show:true}

        },
        {
            path: '*',
            redirect: '/home'
        }
    ],

    //导航置顶
    scrollBehavior(to,from,savePosition){
        return {y:0}
    }
})
// let name = $store.state.login.userInfo.name
router.beforeEach(async(to,from,next)=>{
    let token = localStorage.getItem('user_token')
    let name  = store.state.login.userInfo.loginName
    if(token){
        if(to.path=='/login'){
            next('/home')
        }else{
            if(name){
                next()
            }else{
               try {
                await store.dispatch('abc')
                next()
               } catch (error) {
                //token失效了
               await store.dispatch('logout')
                next('/login')
               }
            }
        }
    }else{
        //未登录
        if(to.path.indexOf('/trade')!=-1||to.path.indexOf('/pay')!=-1||to.path.indexOf('/center')!=-1||to.path.indexOf('/shopcart')!=-1){
            next('/login')
        }else{
            next()
        }
        
        
            
        
        
    }
})
export default router



